package com.aliengen.thingple.thingple;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.aliengen.thingple.thingple.fillingstation.FillingStationActivity;
import com.aliengen.thingple.thingple.tag.OperatorTag;
import com.aliengen.thingple.thingple.tag.Tag;
import com.aliengen.thingple.thingple.tag.exception.InvalidTagException;

/**
 * This class is part of the Thingple mobile application for the CMC168 Handheld reader.
 *
 * Copyright AlienGen Limited 2016. All rights reserved.
 *
 * @author Timothé Mermet-Buffet <tim@aliengen.com>
 */
public class LoginActivity extends AbstractThingpleActivity implements DialogRFIDReadingFragment.ReadingDialogListener {

    DialogRFIDReadingFragment dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        if(mApplication == null || mApplication.mService == null || mApplication.mService.api == null)
        {
            return;
        }

        if(mApplication.mService.api.isAuthenticated())
        {
            Intent nextScreen = new Intent(getApplicationContext(), FillingStationActivity.class);
            startActivity(nextScreen);
            this.finish();
            return;
        }

        dialog = new DialogRFIDReadingFragment();
        dialog.init(this);
    }

    @Override
    public void onResume() {
        super.onResume();

        //dialog = new DialogRFIDReadingFragment();
        //dialog.init(this);
    }

    @Override
    public void onPause() {
        super.onPause();

            try {
                Thread.sleep(200);
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
    }

    public void onClickReadIdentity(View view) {
        dialog.startReading();
    }

    public Tag getTagFromBytes(byte[] tag) throws InvalidTagException
    {
        return new OperatorTag(tag);
    }

    public void onReadingNew(Tag tag) {
        OperatorTag tagop = (OperatorTag) tag;


        System.out.println("Retrieving deviceId...");
        TelephonyManager mngr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = mngr.getDeviceId();

        System.out.println("Authenticating through the API...");
        /* Check RFID Tag. */
        if(!mApplication.mService.api.authenticate(tagop, deviceId))
        {
            this.showToast("User not allowed!");
            System.out.println(mApplication.mService.api.getLastErrors());
            return;
        }

        //Starting a new Intent
        Intent nextScreen = new Intent(getApplicationContext(), FillingStationActivity.class);

        //nextScreen.putExtra("tag_operator", tag);

        //Sending data to another Activity
        //nextScreen.putExtra("name", inputName.getText().toString());
        //nextScreen.putExtra("email", inputEmail.getText().toString());

        // Important when returning to the login activity
        //nextScreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        //Log.e("n", inputName.getText()+"."+ inputEmail.getText());
        startActivity(nextScreen);
        this.finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.login_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            /*
            Snackbar.make(findViewById(R.id.filling_station_main_layout), "Settings of the handheld reader.", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
                    */
            Intent nextScreen = new Intent(getApplicationContext(), com.aliengen.thingple.thingple.SettingsActivity.class);
            startActivity(nextScreen);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
